﻿package plugin.NewSkill.control
{
    import com.core.*;
    import com.greensock.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import manager.*;
    import morn.core.components.*;
    import plugin.NewSkill.consts.*;
    import pluginfw.nio.*;
    import uiresource.skill.*;
    import view.appDrag.*;
    import view.uilt.*;

    public class SkillItemControl extends Object
    {
        private var itemid:int = 209181;
        private var itemid1:int = 2209181;
        public var skillItem:SkillItemUI;
        private var iconCon:Container;
        private var shortBtn:ShortUI;
        private var taleLabel:Label;
        private var power:Image;
        private var simg:Image;
        private var levelTF:Label;
        private var _item:Image;
        private var _data:Object;
        private var rein:int;
        private var upcount:int;
        private var isOther:Boolean;
        private var _isselected:Boolean;
        private var isDown:Boolean;
        private var isCanDown:Boolean;
        private var cfgGuanwei:Object;
        private var cfgSkill:Object;
        private var cfgSkillBook:Object;
        private var _index:int = -1;
        private var _itemId:int = 0;
        private var _skillvalue:int = 0;
        private var _value:int = 0;
        private var image:Image;
        private var timeline3:TweenMax;

        public function SkillItemControl(param1:SkillItemUI)
        {
            this._data = new Object();
            this.cfgGuanwei = ConfigDictionary.data.guanwei;
            this.cfgSkill = ConfigDictionary.data.skill;
            this.cfgSkillBook = ConfigDictionary.data.skillbook;
            this.skillItem = param1;
            this.skillItem.c_skill.visible = false;
            this.skillItem.c_skill.blendMode = BlendMode.ADD;
            this.init();
            this.addListener();
            return;
        }// end function

        private function init() : void
        {
            this.shortBtn = this.skillItem.shortBtn;
            this.taleLabel = this.skillItem.taleLabel;
            this.simg = this.skillItem.sImg;
            this.power = this.skillItem.power;
            this.levelTF = this.skillItem.passivenessLabel;
            this.iconCon = new Container();
            this.iconCon.width = 36;
            this.iconCon.height = 36;
            this.iconCon.x = 11;
            this.iconCon.y = 11;
            this.skillItem.addChild(this.iconCon);
            this.image = new Image();
            this.image.width = 48;
            this.image.height = 48;
            this.iconCon.addChild(this.image);
            return;
        }// end function

        private function addListener() : void
        {
            this.shortBtn.addEventListener(MouseEvent.CLICK, this.clickHandler);
            this.iconCon.addEventListener(MouseEvent.MOUSE_OVER, this.onOverHandler);
            this.iconCon.addEventListener(MouseEvent.MOUSE_MOVE, this.onMoveHandler);
            this.iconCon.addEventListener(MouseEvent.MOUSE_OUT, this.onOutHandler);
            this.iconCon.addEventListener(MouseEvent.MOUSE_DOWN, this.onImgDownHandler);
            this.iconCon.addEventListener(MouseEvent.MOUSE_UP, this.onImgUpHandler);
            this.skillItem.btn_up.addEventListener(MouseEvent.CLICK, this.onUpBtnHandler);
            this.skillItem.addEventListener(MouseEvent.CLICK, this.showSkill);
            return;
        }// end function

        private function showSkill(event:MouseEvent) : void
        {
            ModelProxy.change("skill", "selectskill", this.data);
            return;
        }// end function

        public function openRefresh(param1:Object = null) : void
        {
            return;
        }// end function

        public function set data(param1:Object) : void
        {
            var _loc_2:Object = null;
            var _loc_3:String = null;
            var _loc_4:String = null;
            var _loc_5:int = 0;
            var _loc_6:Object = null;
            var _loc_7:Object = null;
            var _loc_8:String = null;
            var _loc_9:int = 0;
            var _loc_10:Boolean = false;
            var _loc_11:Object = null;
            var _loc_12:Array = null;
            var _loc_13:String = null;
            var _loc_14:Object = null;
            var _loc_15:int = 0;
            var _loc_16:Object = null;
            this._data = param1;
            if (this._data.cfg)
            {
                this._index = -1;
                this._itemId = 0;
                this.isCanDown = true;
                this._data.plus._flag = false;
                _loc_2 = this.cfgSkill[int(this._data.cfg.skillid) + 10];
                if (_loc_2)
                {
                    this.taleLabel.text = int(this._data.cfg.skillProficiency) + "/" + _loc_2.upcount;
                    this._skillvalue = int(_loc_2.upcount) - int(this._data.cfg.skillProficiency);
                    if (int(this._data.cfg.skillProficiency) < int(_loc_2.upcount))
                    {
                        this._data.plus._flag = true;
                        this.skillItem.c_skill.visible = false;
                        this.skillItem.c_skill.stop();
                    }
                    else
                    {
                        this.skillItem.c_skill.visible = true;
                        this.skillItem.c_skill.play();
                    }
                }
                else
                {
                    this.taleLabel.text = "";
                }
                if (this._data.cfg.skillProficiency > 0)
                {
                    this.power.width = this._data.cfg.skillProficiency / _loc_2.upcount * 174;
                }
                else
                {
                    this.power.width = 0;
                }
                if (this.power.width <= 4)
                {
                    this.power.width = 4;
                }
                this.skillItem.visible = true;
                _loc_3 = UIGlobel.getItemd(3);
                _loc_4 = _loc_3 + this._data.cfg.icon + ".jpg";
                this.image.url = Config.getUrl(_loc_4);
                this.skillItem.nameLabel.text = this._data.cfg.name;
                _loc_5 = 0;
                if (this.isOther)
                {
                    _loc_5 = ModelProxy.getValue("data", "otheruser.level");
                }
                else
                {
                    _loc_5 = UserData.getUserData("level");
                }
                _loc_6 = ConfigDictionary.data.skillgroup;
                _loc_7 = _loc_6[this._data.cfg.groupid];
                _loc_8 = MColor.Newstr8;
                _loc_9 = 7;
                _loc_10 = true;
                if (this._data.plus.learn)
                {
                    this.levelTF.text = "等级：" + this._data.cfg.level;
                    if (_loc_7[(int(this._data.cfg.level) + 1)] && int(this._data.cfg.isup) == 1)
                    {
                        this.rein = int(_loc_7[(int(this._data.cfg.level) + 1)].uprein);
                        _loc_15 = UserData.getUserData("rein");
                        if (_loc_15 < this.rein)
                        {
                            _loc_8 = MColor.Newstr9;
                            _loc_10 = false;
                        }
                        _loc_16 = UserData.getBagData();
                        this.getBagIndex(_loc_16, this._data.cfg.skillid);
                    }
                    if (this._data.cfg.skilltype != 0)
                    {
                        this.shortBtn.visible = true;
                    }
                    else
                    {
                        this.shortBtn.visible = false;
                        this.isCanDown = false;
                    }
                    _loc_11 = ModelProxy.getValue("data", "setskill");
                    if (_loc_11 == null)
                    {
                        _loc_11 = new Object();
                        ModelProxy.change("data", "setskill", _loc_11);
                    }
                    _loc_12 = SkillUIControl.STR_ARR;
                    _loc_13 = _loc_12[10];
                    for (_loc_14 in _loc_11)
                    {
                        
                        if (_loc_14 is Number)
                        {
                            if (_loc_11[_loc_14] && _loc_11[_loc_14][0] == 1 && _loc_11[_loc_14][1] == this._data.cfg.skillid)
                            {
                                _loc_13 = _loc_12[(int(_loc_14) - 1)];
                                break;
                            }
                        }
                    }
                    if (this._index > 0 && this._skillvalue > 0)
                    {
                        this.skillItem.btn_up.visible = true;
                    }
                    else
                    {
                        this.skillItem.btn_up.visible = false;
                    }
                }
                else
                {
                    this.isCanDown = false;
                    this.levelTF.text = "等级：0";
                    if (_loc_7[1].studylv > _loc_5)
                    {
                        _loc_8 = MColor.Newstr9;
                    }
                    this.shortBtn.visible = false;
                    this.taleLabel.text = "";
                    this.skillItem.btn_up.visible = false;
                }
                this.shortBtn.keyLabel.text = _loc_13;
                if (this.isOther)
                {
                    this.shortBtn.visible = false;
                }
            }
            return;
        }// end function

        public function play() : void
        {
            if (this.timeline3)
            {
                this.timeline3.kill();
            }
            this.timeline3 = TweenMax.to(this.skillItem.btn_up, 1, {glowFilter:{color:16763904, alpha:1, blurX:6, blurY:6, strength:4}, repeat:int.MAX_VALUE, yoyo:true});
            return;
        }// end function

        public function stop() : void
        {
            if (this.timeline3)
            {
                this.timeline3.kill();
            }
            return;
        }// end function

        public function get data() : Object
        {
            return this._data;
        }// end function

        public function dispose() : void
        {
            this.skillItem.visible = false;
            this._data = null;
            return;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            event.stopPropagation();
            ModelProxy.change("skill", "showkey", {skillid:this._data.cfg.skillid, y:this.skillItem.y + 52});
            return;
        }// end function

        private function onUpBtnHandler(event:MouseEvent) : void
        {
            var _loc_4:Output = null;
            if (this._index == -1)
            {
                return;
            }
            var _loc_2:* = Math.ceil(this._skillvalue / this._value);
            var _loc_3:* = ItemUilt.getAllCount(this._itemId);
            if (_loc_2 < _loc_3)
            {
                _loc_3 = _loc_2;
            }
            if (_loc_3 > 0)
            {
                _loc_4 = new Output();
                _loc_4.writeVarInt(this._itemId, false);
                _loc_4.writeVarInt(_loc_3, false);
                PluginManager.instance.sendToBufferServer("skill", CMDSkill.UseSkillBook, _loc_4);
            }
            return;
        }// end function

        public function get isselected() : Boolean
        {
            return this._isselected;
        }// end function

        public function set isselected(param1:Boolean) : void
        {
            this._isselected = param1;
            if (this._isselected)
            {
                this.simg.visible = true;
            }
            else
            {
                this.simg.visible = false;
            }
            return;
        }// end function

        private function getBagIndex(param1:Object, param2:int) : int
        {
            var _loc_4:Object = null;
            var _loc_5:Object = null;
            var _loc_6:int = 0;
            var _loc_7:String = null;
            var _loc_8:Array = null;
            if (this.isOther)
            {
                return 0;
            }
            var _loc_3:* = Math.floor(param2 / 100);
            this._index = -1;
            this._itemId = 0;
            this._value = 0;
            for (_loc_4 in param1)
            {
                
                _loc_5 = param1[_loc_4];
                if (_loc_5)
                {
                    if (_loc_5.hasOwnProperty("4"))
                    {
                        if (_loc_5[2] == 2)
                        {
                            _loc_6 = _loc_5[1];
                            _loc_5 = _loc_5[4];
                            if (_loc_5 && _loc_5.hasOwnProperty("itemid"))
                            {
                                if (_loc_5.cls == 23)
                                {
                                    this._itemId = _loc_5.itemid;
                                    _loc_7 = _loc_5.skilladd;
                                    _loc_8 = _loc_7.split("#");
                                    if (_loc_8[0] == _loc_3)
                                    {
                                        this._index = int(_loc_4);
                                        this._value = _loc_8[1];
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return this._index;
        }// end function

        private function onOverHandler(event:MouseEvent) : void
        {
            var _loc_2:* = new Point(this.skillItem.stage.mouseX, this.skillItem.stage.mouseY);
            switch(event.currentTarget)
            {
                case this.iconCon:
                {
                    if (this._data)
                    {
                        if (this._data.cfg.skillid)
                        {
                            this._data.plus.wear = 0;
                            ModelProxy.change("tips", "skill", {data:this._data, p:_loc_2});
                        }
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        private function onMoveHandler(event:MouseEvent) : void
        {
            var _loc_2:Image = null;
            var _loc_3:BitmapData = null;
            var _loc_4:Bitmap = null;
            if (this._data)
            {
                if (this._data.cfg.skilltype != 0)
                {
                    if (this.isDown)
                    {
                        _loc_2 = this.iconCon.getChildAt(0) as Image;
                        if (_loc_2)
                        {
                            _loc_3 = new BitmapData(48, 48, true, 0);
                            _loc_3.draw(_loc_2);
                            _loc_4 = new Bitmap(_loc_3);
                            AppDragMgr.clingyItem(this.skillItem, this._data.cfg, _loc_4, 2);
                        }
                        this.isDown = false;
                    }
                }
            }
            return;
        }// end function

        private function onTaleClcikHandler(event:MouseEvent) : void
        {
            var _loc_2:Point = null;
            if (!this.isOther)
            {
                _loc_2 = new Point(this.taleLabel.stage.mouseX, this.taleLabel.stage.mouseY);
                ModelProxy.change("plugin", "change", {target:"sqcs", point:_loc_2, data:2});
            }
            return;
        }// end function

        private function onOutHandler(event:MouseEvent) : void
        {
            ModelProxy.change("tips", "state", "close");
            return;
        }// end function

        private function onImgDownHandler(event:MouseEvent) : void
        {
            if (!this.isOther && this.isCanDown)
            {
                this.isDown = true;
            }
            return;
        }// end function

        private function onImgUpHandler(event:MouseEvent) : void
        {
            this.isDown = false;
            return;
        }// end function

        public function set isSelected(param1:Boolean) : void
        {
            super.isSelected = param1;
            return;
        }// end function

    }
}
